﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 美妆产品管理系统.TGoods
{
    public partial class Insert_Goods : Form
    {
        public Insert_Goods()
        {
            InitializeComponent();
        }
        public static string path_source = "";
        SqlDataAdapter daGood, dalog;
        DataSet ds = new DataSet();
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from Goods ";
            string sdr = "select * from Tlog";
            daGood = new SqlDataAdapter(str, DB.cn);
            dalog = new SqlDataAdapter(sdr, DB.cn);
            daGood.Fill(ds, "Good_info");
            dalog.Fill(ds, "log_info");
        }
        void showAll()
        {
            DataView dvGood = new DataView(ds.Tables["Good_info"]);
            dgvGoods.DataSource = dvGood;
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            if (txtGoodsID.Text == "" || txtUnit.Text == "" || txtName.Text == "" || txtQty.Text == "")
            {
                MessageBox.Show("必填项不能为空");
            }
            else
            {
                DB.GetCn();
                string str = "select * from Goods where Goods_ID='" + txtGoodsID.Text + "'";
                DataTable dt = DB.GetDataSet(str);

                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("此商品已存在，请重新输入商品编号");
                }
                else
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\"
                        + cboCategory.Text + "\\";
                    fileFolder += filename;

                    DataRow drPro = ds.Tables["Good_info"].NewRow();
                    drPro["Goods_ID"] = Convert.ToInt32(txtGoodsID.Text);
                    drPro["Classification_ID"] = cboCategory.SelectedValue;
                    drPro["Unit_Price"] = decimal.Parse(txtPrice.Text);
                    drPro["Cost_Price"] = decimal.Parse(txtUnit.Text);
                    drPro["Supplier_ID"] = cboSupp.SelectedValue;
                    drPro["Goods_Name"] = txtName.Text;
                    drPro["Describe"] = txtDescn.Text;
                    drPro["Stock_Quantity"] = txtQty.Text;
                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);
                        drPro["GImagepath"] = "\\Prod_Images\\" + cboCategory.Text
                            + "\\" + filename;
                    }
                    else
                    {
                        drPro["GImagepath"] = "暂无图片.jpg";
                    }


                    ds.Tables["Good_info"].Rows.Add(drPro);

                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["username"] = Login.username;
                    drLog["type"] = "添加";
                    drLog["action_date"] = DateTime.Now;
                    drLog["action_table"] = "Goods表";
                    ds.Tables["log_info"].Rows.Add(drLog);

                    try
                    {
                        SqlCommandBuilder dbGood = new SqlCommandBuilder(daGood);
                        daGood.Update(ds, "Good_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(dalog);
                        dalog.Update(ds, "log_info");
                        MessageBox.Show("添加成功");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();
                }

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void txtGoodsID_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvGoods_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Insert_Goods_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“salesDataSet1.Supplier”中。您可以根据需要移动或移除它。
            this.supplierTableAdapter.Fill(this.salesDataSet1.Supplier);
            // TODO: 这行代码将数据加载到表“salesDataSet.Category”中。您可以根据需要移动或移除它。
            this.categoryTableAdapter.Fill(this.salesDataSet.Category);
            init();
            showAll();
        }
    }
}
